﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kaiser.Models;
using Kaiser.Modules.Hiner.Models.Order;
using Kaiser.Services;

namespace Kaiser.Repositories.iBatis
{
    public abstract class RepositorieBase<T> : IRepositorieBase<T> where T : class
    {
        protected virtual string Prefix
        {
            get { return typeof(T).Name; }
        }
        protected abstract string MapPath { get; }
        private IBatisHepler _batisHepler;
        protected IBatisHepler BatisHepler
        {

            get
            {
                if (_batisHepler==null)
                {
                    _batisHepler=new BatisHepler(MapPath);
                }
                return _batisHepler;
            }
        }
 

 

        public void Insert(T model)
        {

            BatisHepler.ExcuteInsert(Prefix + ".Insert", model);
        }

        public void Update(T model)
        {

            BatisHepler.ExcuteUpdate(Prefix + ".Update", model);
        }

        public int Delete(object primaryId)
        {

            return BatisHepler.ExcuteDelete(Prefix + ".DeleteList", primaryId);
        }

        public T Get(object primaryId)
        {

            var model = BatisHepler.ExcuteSelectObj(Prefix + ".Get", primaryId) as T;
            return model;
        }

        public int DeleteList(IList<string> ids)
        {

            return BatisHepler.ExcuteUpdate(Prefix + ".DeleteList", ids);
        }

        public virtual Models.DataGrid<T> Search(Object search, Models.PagingInfo pageinfo)
        {
 
            int totalCount = 0;

            var list = BatisHepler.ExcuteSelectForList<T>(Prefix + ".Search", search, pageinfo.Size, pageinfo.Index , out totalCount);


            var result = new DataGrid<T>(list, pageinfo);
            result.total = totalCount;

            return result;
        }
    }
}
